﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HeadPicture
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class D301 : Page
    {
        public D301()
        {
            InitializeComponent();
        }

        int height = 180;
        int width = 180;
        string watermarkImageUrlString = @"http://t3.qpic.cn/mblogpic/e36382577607c8e42270/2000";
        private void PageLoaded(object sender, RoutedEventArgs e)
        {
            string imageurl = @"http://s5.cr.itc.cn/mblog/icon/2b/c7/12964627512447.gif";
            string qqimageurl = @"http://t1.qlogo.cn/mbloghead/5e7ba5c4dab5d55dacca/0";
            
            imgResult.Source = MaskGifImage(imageurl, width, height);
            imgResult.Height = height;
            imgResult.Width = width;
        }
        public ImageSource MaskGifImage(string imageurl, int width, int height)
        {
            var mylogoUrl = new Uri(imageurl);
            var decoder = new GifBitmapDecoder(mylogoUrl, BitmapCreateOptions.PreservePixelFormat,
                                   BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            return MaskImage(width, height, bitmapSource);
        }

        public ImageSource MaskJpegImage(string imageurl, int width, int height)
        {
            var mylogoUrl = new Uri(imageurl);
            var decoder = BitmapDecoder.Create(mylogoUrl, BitmapCreateOptions.PreservePixelFormat,
                       BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            return MaskImage(width, height, bitmapSource);
        }

        public ImageSource MaskImage(int width, int height, BitmapSource originalSource)
        {
            var drawingVisual = new DrawingVisual();
            var drawingContext = drawingVisual.RenderOpen();

            // Draw original image
            drawingContext.DrawImage(originalSource, new Rect(0.0, 0.0, width, height));

            // Draw watermark
            var watermarkUrl = new Uri(watermarkImageUrlString);
            var watermarkImage = new PngBitmapDecoder(watermarkUrl, BitmapCreateOptions.PreservePixelFormat,
                                                      BitmapCacheOption.Default);
            BitmapSource watermarkSource = watermarkImage.Frames[0];
            drawingContext.DrawImage(watermarkSource, new Rect(0.0, 0.0, 95, 95));

            drawingContext.Close();

            // Render target bitmap
            var renderTargetBitmap = new RenderTargetBitmap(width, height,
                0, 0, PixelFormats.Pbgra32);
            renderTargetBitmap.Render(drawingVisual);

            return renderTargetBitmap;
        }

        static public void SaveToBmp()
        {
            int width = 128;
            int height = width;
            int stride = width / 8;
            byte[] pixels = new byte[height * stride];

            // Try creating a new image with a custom palette.
            List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>();
            colors.Add(System.Windows.Media.Colors.Red);
            colors.Add(System.Windows.Media.Colors.Blue);
            colors.Add(System.Windows.Media.Colors.Green);
            BitmapPalette myPalette = new BitmapPalette(colors);

            // Creates a new empty image with the pre-defined palette
            BitmapSource image = BitmapSource.Create(
                width,
                height,
                96,
                96,
                PixelFormats.Indexed1,
                myPalette,
                pixels,
                stride);

            //var image2 = BitmapSource.Create()
            Uri myUri = new Uri(@"C:\dev\tvstack_test\Tools\HeadPicture\TestResults\peli_PELI2 2011-07-27 17_39_44\Out\new.bmp", UriKind.RelativeOrAbsolute);
            BmpBitmapDecoder decoder2 = new BmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource2 = decoder2.Frames[0];


            FileStream stream = new FileStream("new.bmp", FileMode.Create);
            BmpBitmapEncoder encoder = new BmpBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bitmapSource2));//image));
            encoder.Save(stream);
        }
    }
}
